package net.realsee.service.wcs.device.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import net.realsee.common.utils.StringUtils;
import net.realsee.domain.wcs.device.WcsEquipment;
import net.realsee.domain.wcs.device.bo.WcsEquipmentBo;
import net.realsee.domain.wcs.device.vo.WcsEquipmentVo;
import net.realsee.framework.core.domain.PageQuery;
import net.realsee.framework.core.page.TableDataInfo;
import net.realsee.mapper.wcs.device.WcsEquipmentMapper;
import net.realsee.service.wcs.device.IWcsEquipmentService;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 设备信息Service业务层处理
 *
 * @author realsee
 * @date 2023-08-11
 */
@RequiredArgsConstructor
@Service
public class WcsEquipmentServiceImpl extends ServiceImpl<WcsEquipmentMapper, WcsEquipment> implements IWcsEquipmentService {

    private final WcsEquipmentMapper baseMapper;

    /**
     * 查询设备信息
     */
    @Override
    public WcsEquipmentVo selectVoById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询设备信息列表
     */
    @Override
    public TableDataInfo<WcsEquipmentVo> selectVoPage(WcsEquipmentBo bo, PageQuery pageQuery) {
        IPage<WcsEquipmentVo> result = baseMapper.queryDataByPage(pageQuery.build(), bo);
//        LambdaQueryWrapper<WcsEquipment> lqw = buildVoQueryWrapper(bo);
//        Page<WcsEquipmentVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询设备信息列表
     */
    @Override
    public List<WcsEquipmentVo> selectVoList(WcsEquipmentBo bo) {
        LambdaQueryWrapper<WcsEquipment> lqw = buildVoQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<WcsEquipment> buildVoQueryWrapper(WcsEquipmentBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<WcsEquipment> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getId() != null, WcsEquipment::getId, bo.getId());
        lqw.eq(StringUtils.isNotBlank(bo.getEquipment()), WcsEquipment::getEquipment, bo.getEquipment());
        lqw.eq(StringUtils.isNotBlank(bo.getEquipmentType()), WcsEquipment::getEquipmentType, bo.getEquipmentType());
        lqw.like(StringUtils.isNotBlank(bo.getEquipmentName()), WcsEquipment::getEquipmentName, bo.getEquipmentName());
        lqw.eq(StringUtils.isNotBlank(bo.getWorkCenter()), WcsEquipment::getWorkCenter, bo.getWorkCenter());
        lqw.eq(StringUtils.isNotBlank(bo.getCommType()), WcsEquipment::getCommType, bo.getCommType());
        lqw.eq(StringUtils.isNotBlank(bo.getCommAddr()), WcsEquipment::getCommAddr, bo.getCommAddr());
        lqw.eq(bo.getCommPort() != null, WcsEquipment::getCommPort, bo.getCommPort());
        lqw.eq(StringUtils.isNotBlank(bo.getFuid()), WcsEquipment::getFuid, bo.getFuid());
        lqw.eq(bo.getTextId() != null, WcsEquipment::getTextId, bo.getTextId());
        lqw.eq(bo.getRefrenceId() != null, WcsEquipment::getRefrenceId, bo.getRefrenceId());
        lqw.eq(bo.getUnitId() != null, WcsEquipment::getUnitId, bo.getUnitId());
        lqw.eq(bo.getDelFlag() != null, WcsEquipment::getDelFlag, bo.getDelFlag());
        lqw.eq(StringUtils.isNotBlank(bo.getCreateBy()), WcsEquipment::getCreateBy, bo.getCreateBy());
        lqw.eq(bo.getCreateTime() != null, WcsEquipment::getCreateTime, bo.getCreateTime());
        lqw.eq(StringUtils.isNotBlank(bo.getUpdateBy()), WcsEquipment::getUpdateBy, bo.getUpdateBy());
        lqw.eq(bo.getUpdateTime() != null, WcsEquipment::getUpdateTime, bo.getUpdateTime());
        lqw.eq(StringUtils.isNotBlank(bo.getDeleteBy()), WcsEquipment::getDeleteBy, bo.getDeleteBy());
        lqw.eq(bo.getDeleteTime() != null, WcsEquipment::getDeleteTime, bo.getDeleteTime());
        lqw.eq(bo.getRowVersionStamp() != null, WcsEquipment::getRowVersionStamp, bo.getRowVersionStamp());
        lqw.eq(bo.getVersion() != null, WcsEquipment::getVersion, bo.getVersion());
        lqw.eq(StringUtils.isNotBlank(bo.getRemark()), WcsEquipment::getRemark, bo.getRemark());
        return lqw;
    }

    /**
     * 新增设备信息
     */
    @Override
    public Boolean insertByBo(WcsEquipmentBo bo) {
        WcsEquipment add = BeanUtil.toBean(bo, WcsEquipment.class);
        return this.save(add);
    }

    /**
     * 修改设备信息
     */
    @Override
    public Boolean updateByBo(WcsEquipmentBo bo) {
        WcsEquipment update = BeanUtil.toBean(bo, WcsEquipment.class);
        return this.updateById(update);
    }

    /**
     * 批量删除设备信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids) {
        return this.removeBatchByIds(ids);
    }
}
